<?php
require (Yii::app ()->basePath . '/lib/rolling-curl/RollingCurl.php');
/**
 *
 * @author Greening
 * @see 绿化监督管理
 */
class GreeningController extends CController {
	
	/**
	 *
	 * @see 绿化监督管理-绿地监督列表
	 */
	function actionList() {
		$this->render ( 'list' );
	}
	
	/**
	 *
	 * @see ajax 绿地监督列表数据
	 */
	function actionAjax_green_list() {
		$ret = new stdClass ();
		$ret->status = 401;
		$service = new GreeningEntity ();
		$pg = $_GET ['page'];
		// 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$where = array ();
		$pname = $_GET ['pname'];
		$sfwg = $_GET ['sfwg'];
		$tb = $_GET ["tb"];
		if ($pname != "") {
			$where ['p_name'] = "[code] like '%$pname%' [code]";
		}
		if ($sfwg != "") {
			$where ['sfwg'] = $sfwg;
		}
		
		switch ($tb) {
			default :
				break;
			case "1" :
				$where ['status'] = "[code]<>'达标办结'[code]";
				break;
			case "2" :
				$where ['status'] = "达标办结";
				break;
		}
		$list = $service->get_list ( $pg, 10, $where, array (
				'id' => 'desc' 
		) );
		$tct_where = $where;
		$dbct_where = $where; // 达标办结\
		$dbct_where ['status'] = "达标办结";
		$wbj_where = $where; // 未办结
		$wbj_where ['status'] = "[code]<>'达标办结'[code]";
		
		$totalCount = $service->get_record_count ( $where );
		$ret->tct = $totalCount;
		$ret->db_ct = $service->get_record_count ( $dbct_where );
		$ret->wbj_ct = $service->get_record_count ( $wbj_where );
		
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		if ($list === false) {
			$ret->status = 0;
			$ret->data = "未找到匹配数据";
		} else {
			$ret->status = 1;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 达标办结
	 */
	function actionAjax_dabiao_banjie() {
		$id = $_POST ['id'];
		$service = new GreeningEntity ();
		$service->update ( array (
				'status' => '达标办结' 
		), array (
				'id' => $id 
		) );
	}
	
	/**
	 *
	 * @see 绿化监督管理-绿地监督列表-增加界面
	 */
	function actionAdd() {
		$this->render ( 'add' );
	}
	
	/**
	 *
	 * @see 绿化监督管理 ajax提交
	 *     
	 */
	public function actionAjax_green_add() {
		$ret = new stdClass ();
		$ret->status = 401;
		
		$c_unit = $_POST ['unit'];
		$p_name = $_POST ['name'];
		$p_address = $_POST ['p_address'];
		$docket_no = $_POST ['docket_no'];
		$jy_area = $_POST ['jy_area'];
		$sh_area = $_POST ['sh_area'];
		$shgy_area = $_POST ['shgy_area'];
		$ghylld_area = $_POST ['ghylld_area'];
		$ghylgyld_area = $_POST ['ghylgyld_area'];
		$lhsg_unit = $_POST ['lhsg_unit'];
		$sgwd_linkman = $_POST ['sgwd_linkman'];
		$sgdw_phone = $_POST ['sgdw_phone'];
		$sj_date = $_POST ['sj_date'];
		$linkman = $_POST ['linkman'];
		$linkphone = $_POST ['linkphone'];
		$sfwg = $_POST ['sfwg'];
		$fldvalue = array (
				'c_unit' => $c_unit,
				'p_name' => $p_name,
				'p_address' => $p_address,
				'docket_no' => $docket_no,
				'jy_area' => $jy_area,
				'sh_area' => $sh_area,
				'shgy_area' => $shgy_area,
				'ghylld_area' => $ghylld_area,
				'ghylgyld_area' => $ghylgyld_area,
				'lhsg_unit' => $lhsg_unit,
				'sgwd_linkman' => $sgwd_linkman,
				'sgdw_phone' => $sgdw_phone,
				'sj_date' => $sj_date,
				'linkman' => $linkman,
				'linkphone' => $linkphone,
				'sfwg' => $sfwg,
				'bj_date' => '0000-00-00',
				'add_time' => date ( 'Y-m-d', time () ),
				'deleted' => 0,
				'status' => '未办结' 
		);
		$service = new GreeningEntity ();
		$result = $service->add ( $fldvalue );
		// 'register_time' => date ( 'Y-m-d H:i:s', time () )
		if (is_numeric ( $result ) && $result > 0) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see ajax更新
	 *
	 */
	public function actionAjax_update() {
		$ret = new stdClass ();
		$ret->status = 0;
		/*
		 * $id = $_POST ['id'];
		 * $c_unit = $_POST ['c_unit'];
		 * $p_name = $_POST ['p_name'];
		 * $p_address = $_POST ['p_address'];
		 * $rpgs_area = $_POST ['rpgs_area'];
		 * $rppgs_area = $_POST ['rppgs_area'];
		 * $rg_area = $_POST ['rg_area'];
		 * $rpg_area = $_POST ['rpg_area'];
		 * $results = $_POST ['results'];
		 * $date = $_POST ['date'];
		 * $fldvalue = array (
		 * 'shenghe' => '1',
		 * 'c_unit' => $c_unit,
		 * 'p_name' => $p_name,
		 * 'p_address' => $p_address,
		 * 'rpgs_area' => $rpgs_area,
		 * 'rppgs_area' => $rppgs_area,
		 * 'rg_area' => $rg_area,
		 * 'rpg_area' => $rpg_area,
		 * 'results' => $results,
		 * 'date' => $date
		 * );
		 * $greeningService = new greeningEntity ();
		 * $result = $greeningService->update_greening ( $fldvalue, array (
		 * 'id' => $id
		 * ) );
		 * //
		 *
		 * if ($result) {
		 * $ret->status = 1;
		 * }
		 */
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 绿化监督管理-绿地监督列表-实施情况确认登记
	 */
	function actionRegistration() {
		$id = $_GET ['id'];
		$service = new GreeningEntity ();
		$model = $service->get ( $id );
		$this->render ( 'registration', array (
				'id' => $id,
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 确认登记
	 */
	function actionAjax_green_registration() {
		$ret = new stdClass ();
		$ret->status = 401;
		$id = $_POST ['id'];
		$content = $_POST ['content'];
		$slld_area = $_POST ['slld_area'];
		$slgyld_area = $_POST ['slgyld_area'];
		$jg_time = $_POST ['jg_time'];
		$service = new GreeningEntity ();
		$reulst = $service->update ( array (
				'qr_remark' => $content,
				'slld_area' => $slld_area,
				'slgyld_area' => $slgyld_area,
				'status' => '确认登记',
				'jg_time' => $jg_time 
		), array (
				'id' => $id 
		) );
		if ($reulst === true) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 审核登记
	 */
	function actionAjax_green_opinion() {
		$ret = new stdClass ();
		$ret->status = 401;
		$id = $_POST ['id'];
		$content = $_POST ['content'];
		$service = new GreeningEntity ();
		$reulst = $service->update ( array (
				'sh_remark' => $content,
				'status' => '审核登记' 
		), array (
				'id' => $id 
		) );
		if ($reulst === true) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 绿化监督管理-绿地监督列表-处理意见审核核算及确认
	 */
	function actionOpinion() {
		$id = $_GET ['id'];
		$service = new GreeningEntity ();
		$model = $service->get ( $id );
		$this->render ( 'opinion', array (
				'id' => $id,
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 绿化监督管理-绿地监督列表-打印监督检查报告
	 */
	function actionPrint() {
		$id = $_GET ['id'];
		$service = new GreeningEntity ();
		$model = $service->get ( $id );
		$this->render ( 'print', array (
				'id' => $id,
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 绿化监督管理-新登记项目统计
	 */
	function actionNew() {
		$this->render ( 'new' );
	}
}
